var chargingRecordStatisticsData = [];
var totalPage = 0;
var totalCnt = 0;
var orderList = [];
var lastheaderNameEN = undefined;
var formData = undefined;

//查询
$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("chargingRecordStatisticsThead", chargingRecordStatisticsTable, search);
	scrollTableSuspend("scrollBody", "chargingRecordTable", "chargingRecordTable1", "chargingRecordStatisticsThead", "scrollHeight");
	selectDate("startDate", 0);
	selectDate("endDate", 0);
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
});
function getChargingStation(data) {
	$("#startChargingTime").val(date2String(getSystemTime().getTime()) + " 00:00:00");
	$("#endChargingTime").val(date2String(getSystemTime().getTime()) + " 23:59:59");
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
	search();
}
function changeEquipmet(){
	var chargingStationID = $("#chargingStationID").val();
	if (chargingStationID) {
		$("#changeEquipment").show();
		$("#equipmentNo").empty();
		$ajaxList('equipmentNo', getEquipmentNo, {"chargingStationID": chargingStationID});
	} else {
		$("#changeEquipment").hide();
		$("#equipmentNo").empty();
	}
}
function getEquipmentNo(data) {
	var length = data.length;
	$("#equipmentNo").append("<option value=''>请选择</option>");
	for (var i = 0; i < length; i++) {
		$("#equipmentNo").append("<option value='" + data[i].equipmentNo + "'>" + data[i].equipmentName + "</option>");
	}
	if(formData){
		if(formData.equipmentNo != "") {
			$("#equipmentNo").val(formData.equipmentNo);
		}
	}
}
function search(headerNameEN){
	if($("#detailID", parent.document).val() == "backFlag"){
		$("#searchType").val("detail");
		$("#detailID", parent.document).val("");
	}
	var startChargingTime = $("#startChargingTime").val();
	var endChargingTime = $("#endChargingTime").val();
	if(lastheaderNameEN != headerNameEN){
		clearOrder(lastheaderNameEN);
		lastheaderNameEN = headerNameEN;
	}
	if (stringToDate(startChargingTime) > stringToDate(endChargingTime)){
		showMessge("开始时间不能大于结束时间！");
	} else {
		$ajax('searchChargingRecordStatistics', 'chargingRecordStatisticsSearch', searchChargingRecordStatistics);
	}
}
function searchChargingRecordStatistics(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	if(data.chargingRecord){
		formData = data && data.chargingRecord;
		if(formData.chargingStationID != "") {
			$("#chargingStationID").val(formData.chargingStationID);
			changeEquipmet();
		}
		if(formData.startChargingTime != "") {
			$("#startChargingTime").val(formData.startChargingTime);
		}
		if(formData.endChargingTime != "") {
			$("#endChargingTime").val(formData.endChargingTime);
		}
		$("#searchType").val("");
	}
	chargingRecordStatisticsData = data && data.chargingRecordStatisticsList || [];
	createTableBody("chargingRecordStatisticsTbody", chargingRecordStatisticsTable, chargingRecordStatisticsData);
	var totalChargingCount = 0;
	var totalChargingKWH = 0;
	var totalAttritionKWH = 0;
	for(var i = 0; i < chargingRecordStatisticsData.length; i++){
		totalChargingCount += chargingRecordStatisticsData[i].chargingCount;
		totalChargingKWH += chargingRecordStatisticsData[i].totalChargingKWH;
		totalAttritionKWH += chargingRecordStatisticsData[i].attritionAmmeterNo;
	}
	$('#chargingRecordStatisticsTbody').append('<tr><th>合计</th><th></th><th></th><th></th><th></th><th></th><th>' + totalChargingCount + '</th><th>' + totalChargingKWH + '</th><th>' + totalAttritionKWH + '</th><th></th><th></th></tr>');
	setTimeout(hideLoading, 500);
}
function details(index){
	if($('#chargingStationID').val() != ""){
		$('#parameter').val(1);
	}
	window.parent.detalisSkip('chargingEquipment/chargingRecordStatisticsDetail');
	$("#detailID", parent.document).val(chargingRecordStatisticsData[index].equipmentNo);
	$("#startTime", parent.document).val($("#startChargingTime").val());
	$("#endTime", parent.document).val($("#endChargingTime").val());
}
